package com.ruoyi.ebike.controller;

import java.io.Console;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ebike.domain.Orders;
import com.ruoyi.ebike.service.IOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 骑行记录Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/ebikeUser/orders")
public class OrdersController extends BaseController
{
    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询骑行记录列表
     */
    @PreAuthorize("@ss.hasPermi('ebikeUser:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(Orders orders)
    {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        if (list == null || list.isEmpty()) {
            System.out.println("list is empty or null");
            return getDataTable(new ArrayList<>());
        }

        return getDataTable(list);
    }

    /**
     * 导出骑行记录列表
     */
    @PreAuthorize("@ss.hasPermi('ebikeUser:orders:export')")
    @Log(title = "骑行记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders)
    {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        util.exportExcel(response, list, "骑行记录数据");
    }

    /**
     * 获取骑行记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ebikeUser:orders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ordersService.selectOrdersById(id));
    }
    @PreAuthorize("@ss.hasPermi('ebikeUser:orders:query')")
    @GetMapping("last/{vehicleId}")
    public AjaxResult getLastInfo(@PathVariable("vehicleId") Long vehicleId)
    {

        return success(ordersService.selectOrdersByVehicleId(vehicleId));
    }

    /**
     * 新增骑行记录
     */
    @PreAuthorize("@ss.hasPermi('ebikeUser:orders:add')")
    @Log(title = "骑行记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改骑行记录
     */
    @PreAuthorize("@ss.hasPermi('ebikeUser:orders:edit')")
    @Log(title = "骑行记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders)
    {
        return toAjax(ordersService.updateOrders(orders));
    }



    /**
     * 删除骑行记录
     */
    @PreAuthorize("@ss.hasPermi('ebikeUser:orders:remove')")
    @Log(title = "骑行记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ordersService.deleteOrdersByIds(ids));
    }



    /**
     * 统计总收入
     */
    @GetMapping("/fetchOrderStats")
    public Map<String, BigDecimal> fetchOrderStats(@RequestParam String timeRange) {
        return ordersService.getOrderStats(timeRange);
    }

    @GetMapping("/getOrderCount")
    public Map<String, BigDecimal> fetchOrderCount(@RequestParam String timeRange) {
        return ordersService.getOrderCount(timeRange);
    }

    @GetMapping("/getRidingDistance")
    public BigDecimal getRidingDistance() {
        return ordersService.getTotalRidingDistance();
    }


}
